<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <!-- 左侧logo区域 -->
      <div class="logo">
        <!-- logo图片 -->
        <img src="../assets/img/heima.png" alt="" />
        <!-- logo文字 -->
        <span>电商后台管理系统</span>
      </div>
      <!-- 右侧操作区域 -->
      <el-dropdown class="right-menu">
        <span class="el-dropdown-link">
          {{username}} 你好！<i class="el-icon-arrow-down el-icon--right"></i>
        </span>
        <!-- 下拉菜单 -->
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item>全屏</el-dropdown-item>
          <!-- 很多时候一些第三方的UI库组件，组件标签上面添加原生事件是无效的！ -->
          <!-- 要解决：很简单添加上.native 事件修饰符 -->
          <el-dropdown-item @click.native="quit">退出</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="iscollapse ? '64px':'230px' ">
        <!-- 切换按钮 -->
        <div class="toggle">
          <i class="el-icon-s-unfold" @click="iscollapse=!iscollapse" v-if="iscollapse"></i>
          <i class="el-icon-s-fold" @click="iscollapse=!iscollapse" v-else></i>
        </div>
        <!-- 菜单内容 -->
        <!-- router模式，一旦开启，会把index的内容作为地址去跳转！ -->
        <!-- 
          default-active是和index关联！如果和index内容一致，那么该菜单就会高亮！
         -->
        <el-menu
          :default-active="$route.path"
          background-color="#545c64"
          text-color="#fff"
          router
          :collapse="iscollapse"
          :collapse-transition="false"
        >
          <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item v-for="val in item.children" :index="'/'+val.path" :key="val.id">
                {{val.authName}} 
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容区域 -->
      <el-main> 
        <!-- 二级路由出口！ -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
    data() {
        //从本地存储中读取
        let { username  } = JSON.parse(sessionStorage.getItem("SHOP_USERINFO"))
        return {
            menulist:[], // 菜单列表数据
            // 图标数据
            iconsObj: {
                '125':'iconfont icon-user',
                '103':'iconfont icon-tijikongjian',
                '101':'iconfont icon-shangpin',
                '102':'iconfont icon-danju',
                '145':'iconfont icon-baobiao'
            },
            // 是否收起侧边栏，默认不收起
            iscollapse:false,
            // 用户昵称
            username
        }
    },
    created () {
        this.getMenuList()
    },
    methods: {
        //   获取菜单
        async getMenuList(){
            // 请求发送
            let { data:res } = await this.$axios.get("menus")
            // 错误处理
            if(res.meta.status !== 200) return this.$message.error(this.meta.msg)
            // 成功赋值数据
            this.menulist = res.data
        },
        // 退出登录
        quit() {
        // 删除本地存储数据，跳转会登录页面
        sessionStorage.removeItem("SHOP_USERINFO");
        this.$router.push("/login");
        }
    },
};
</script>

<style lang="less" scoped>
.home-container {
  height: 100vh;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  .logo {
    display: flex;
    align-items: center;
    img {
      height: 50px;
      margin-right: 10px;
    }
    span {
      color: #fff;
      font-size: 20px;
    }
  }
  .right-menu {
    .el-dropdown-link {
      color: #fff;
      line-height: 60px;
    }
  }
}

.el-aside {
  background-color: #333744;
  .toggle {
    text-align: center;
    color: #fff;
    line-height: 30px;
    background-color: #6a6d79;
    cursor: pointer;
  }
}
.el-main {
  background-color: #eaedf1;
}
.el-menu{
    border-right: none;
     .iconfont{
        color: #fff;
        margin-right: 5px;
    }
}
.el-main{
  box-sizing: border-box;
  height: calc(100vh - 60px);
}
</style>